﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
using BL;
using DA;
using Entities;
using System.Net.Mail;
using System.Net;


namespace ERP
{
    public partial class OrderStatusForm : System.Web.UI.Page
    {
        OrderManager orderManager = new OrderManager();
        int? orderId;

        protected void Page_Load(object sender, EventArgs e)
        {
            orderId = Request.QueryString.Get("OrderId").ToInt();
            
            if (!IsPostBack)
            {
                if (orderId.HasValue)
                {
                    ERP_Orders order = orderManager.GetOrder(orderId.Value);

                    if (order != null)
                    {
                        labelOrderID.Text = order.OrderId.ToString();
                        labelUser.Text = order.ERP_Users.EMail;
                        
                        if (order.ERP_Suppliers != null)
                            labelSupplier.Text = order.ERP_Suppliers.Name;
                        else
                            labelSupplier.Text = "ללא ספק";

                        labelDetails.Text = order.OrderDetails;

                        if(order.Price.HasValue)
                            labelPrice.Text = order.Price.Value.ToString("0.00");
                        else
                            labelPrice.Text = "לא מוגדר";

                        labelPaymentMethod.Text = order.PaymentMethod;
                        labelOrderDate.Text = order.CreationDate.ToShortDateString();

                        OrderStatus status;
                        if (order.OrderStatus.HasValue)
                        {
                            if (Enum.TryParse(order.OrderStatus.Value.ToString(), out status))
                            {
                                labelCurrentStatus.Text = Enum.GetName(typeof(OrderStatus), status);
                            }
                            else
                            {
                                labelCurrentStatus.Text = "שגיאה. לא ניתן להציג את הסטטוס.";
                            }
                        }
                        else
                        {
                            labelCurrentStatus.Text = "לא מוגדר";
                        }

                        TextBoxManagerNotes.Text = order.ManagerNotes;
                    }
                    else
                    {
                        ShowError("The order was not found.");
                    }
                }
            }
        }

        public void buttonApprove_OnClick(object sender, EventArgs e)
        {

            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential("ERP@ccn.org.il", "0506597860"),
                EnableSsl = true
            };
            client.Send("ERP@ccn.org.il", "ERP@ccn.org.il", "test", "testbody");
            
            if (orderId.HasValue)
            {
                ERP_Orders order = orderManager.GetOrder(orderId.Value);

                if (order != null)
                {
                    if (orderManager.EditOrderStatus(order.OrderId, (int)OrderStatus.מאושר, TextBoxManagerNotes.Text))
                    {

                        Response.Redirect("OrderStatusResponse.aspx?Message=Approved");
                    }
                    else
                    {
                        ShowError("שגיאה, לא ניתן לעדכן את ההזמנה.");
                    }
                }
                else
                {
                    ShowError("שגיאה. ההזמנה לא נמצאה.");
                }
            }
        }

        public void buttonDecline_OnClick(object sender, EventArgs e)
        {
            if (orderId.HasValue)
            {
                ERP_Orders order = orderManager.GetOrder(orderId.Value);

                if (order != null)
                {
                    if (orderManager.EditOrderStatus(order.OrderId, (int)OrderStatus.נדחה, TextBoxManagerNotes.Text))
                    {
                        Response.Redirect("OrderStatusResponse.aspx?Message=Declined");
                    }
                    else
                    {
                        ShowError("שגיאה, לא ניתן לעדכן את ההזמנה.");
                    }
                }
                else
                {
                    ShowError("שגיאה. ההזמנה לא נמצאה.");

                }
            }
        }

        private void ShowError(string message)
        {
            this.lblMessage.Text = message;
            this.lblMessage.Visible = true;
        }

    }
}